package com.xiaohua.api.utils.file;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 文件类型工具类
 * 
 * @author 小花
 * @date 2025-03-29
 */
public class FileTypeUtils {
    
    /** 图片扩展名列表 */
    private static final List<String> IMAGE_EXTENSIONS = Arrays.asList(
            "jpg", "jpeg", "png", "gif", "bmp");
    
    /** PDF扩展名 */
    private static final String PDF_EXTENSION = "pdf";
    
    /**
     * 判断文件是否为图片
     * 
     * @param filename 文件名
     * @return 是否为图片
     */
    public static boolean isImage(String filename) {
        if (StringUtils.isBlank(filename)) {
            return false;
        }
        
        String extension = FilenameUtils.getExtension(filename.toLowerCase());
        return IMAGE_EXTENSIONS.contains(extension);
    }
    
    /**
     * 判断文件是否为PDF
     * 
     * @param filename 文件名
     * @return 是否为PDF
     */
    public static boolean isPdf(String filename) {
        if (StringUtils.isBlank(filename)) {
            return false;
        }
        
        String extension = FilenameUtils.getExtension(filename.toLowerCase());
        return PDF_EXTENSION.equalsIgnoreCase(extension);
    }
    
    /**
     * 判断文件是否为Word格式（DOC或DOCX）
     *
     * @param fileName 文件名
     * @return true如果是Word文件，否则false
     */
    public static boolean isWord(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return false;
        }
        String lowerCase = fileName.toLowerCase();
        return lowerCase.endsWith(".doc") || lowerCase.endsWith(".docx");
    }
    
    /**
     * 获取文件的MIME类型
     * 
     * @param filename 文件名
     * @return MIME类型
     */
    public static String getMimeType(String filename) {
        if (StringUtils.isBlank(filename)) {
            return "application/octet-stream";
        }
        
        String extension = FilenameUtils.getExtension(filename.toLowerCase());
        
        switch (extension) {
            case "pdf":
                return "application/pdf";
            case "doc":
                return "application/msword";
            case "docx":
                return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            case "xls":
                return "application/vnd.ms-excel";
            case "xlsx":
                return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            case "ppt":
                return "application/vnd.ms-powerpoint";
            case "pptx":
                return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            case "jpg":
            case "jpeg":
                return "image/jpeg";
            case "png":
                return "image/png";
            case "gif":
                return "image/gif";
            case "bmp":
                return "image/bmp";
            default:
                return "application/octet-stream";
        }
    }
    
    /**
     * 获取文件扩展名（不含点）
     *
     * @param fileName 文件名
     * @return 文件扩展名，如果没有扩展名则返回空字符串
     */
    public static String getFileExtension(String fileName) {
        if (fileName == null || fileName.isEmpty() || !fileName.contains(".")) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
    
    /**
     * 根据文件扩展名判断是否为允许上传的文件类型
     *
     * @param fileName 文件名
     * @param allowedExtensions 允许的扩展名数组
     * @return true如果是允许的文件类型，否则false
     */
    public static boolean isAllowedFileType(String fileName, String[] allowedExtensions) {
        if (fileName == null || fileName.isEmpty() || allowedExtensions == null || allowedExtensions.length == 0) {
            return false;
        }
        
        String extension = getFileExtension(fileName).toLowerCase();
        for (String allowedExt : allowedExtensions) {
            if (extension.equals(allowedExt.toLowerCase())) {
                return true;
            }
        }
        
        return false;
    }
} 